package com.kuyu.media;

import com.kuyu.json.JsonUtils;
import com.kuyu.model.WeixinMedia;
import com.kuyu.model.WxAccessToken;
import com.kuyu.model.WxFinalValue;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.reflect.FieldUtils;
import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

public class MediaKit {
    public static String postMedia(String path,String type){
        CloseableHttpClient client=null;
        CloseableHttpResponse response=null;
        try {
            //设置请求URL
            String url= WxFinalValue.POST_MEDIA_URL;
            url=url.replace("ACCESS_TOKEN", WxAccessToken.getAccessToken());
            url=url.replace("TYPE",type);
            // 定义HttpClient
            client= HttpClients.createDefault();
            // 实例化HTTP方法
            HttpPost post=new HttpPost(url);
            //设置参数,区分上传文件和传参方式不同
            FileBody fb=new FileBody(new File(path));
            HttpEntity httpEntity= MultipartEntityBuilder.create().addPart("media",fb).build();
            post.setEntity(httpEntity);
            //发送post/get请求
            response=client.execute(post);
            /**请求发送成功，并得到响应**/
            int sc=response.getStatusLine().getStatusCode();
            if(sc>=200&&sc<300){
                /**读取服务器返回过来的json字符串数据**/
                String json= EntityUtils.toString(response.getEntity());
                //将json格式串转为对象
                WeixinMedia media= JsonUtils.jsonToPojo(json,WeixinMedia.class);
                System.out.println(media.getMedia_id());
                return media.getMedia_id();
            }
        } catch (ClientProtocolException e){
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e){
            e.printStackTrace();
        }
        finally {
            if(client!=null) try {
                client.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            if(response!=null) try {
                response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
    //根据mid获取图片
    public static void getMedia(String mid,File f){
        CloseableHttpClient client=null;
        CloseableHttpResponse response=null;

        try {
            String url=WxFinalValue.GET_MEDIA_URL;
            url=url.replace("ACCESS_TOKEN", WxAccessToken.getAccessToken());
            url=url.replace("MEDIA_ID",mid);
            client= HttpClients.createDefault();
            HttpGet get=new HttpGet(url);
            response=client.execute(get);
            /**请求发送成功，并得到响应**/
            int sc=response.getStatusLine().getStatusCode();
            if(sc>=200&&sc<300){
                /**读取服务器返回过来的json字符串数据**/
               InputStream is= response.getEntity().getContent();
                FileUtils.copyInputStreamToFile(is,f);
            }
        } catch (ClientProtocolException e){
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e){
            e.printStackTrace();
        }
        finally {
            if(client!=null) try {
                client.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            if(response!=null) try {
                response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
